package com.chuantou.yuba.api;

import java.io.File;
import java.util.HashMap;

import android.content.Context;
import android.text.TextUtils;

import com.android.volley.Network;
import com.android.volley.Request;
import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.BasicNetwork;
import com.android.volley.toolbox.DiskBasedCache;
import com.android.volley.toolbox.Volley;
import com.chuantou.yuba.api.request.BaseGsonRequest;
import com.chuantou.yuba.api.request.GsonRequest;
import com.chuantou.yuba.api.request.MultiPartRequest;
import com.chuantou.yuba.api.request.MultiPartStack;
import com.chuantou.yuba.api.to.BaseTO;
import com.chuantou.yuba.api.to.LoginTO;
import com.chuantou.yuba.api.to.TrendsDataTO;
import com.chuantou.yuba.api.to.UpdateAvatarDataTO;
import com.chuantou.yuba.api.to.UpdateCoverDataTO;
import com.chuantou.yuba.api.to.UserTO;
import com.chuantou.yuba.util.Constants;
import com.chuantou.yuba.util.MD5Util;

public class RequestManager {
	private static RequestQueue sRequestQueue;
	private static RequestQueue sUploadQueue;

	private static synchronized RequestQueue getRequestQueue(Context context) {
		return sRequestQueue == null ? sRequestQueue = Volley.newRequestQueue(context, null, ".http") : sRequestQueue;
	}

	private static synchronized RequestQueue getUploadRequestQueue(Context context) {
		if (sUploadQueue == null) {
			File cacheDir = new File(context.getCacheDir(), "volley");
			Network network = new BasicNetwork(new MultiPartStack());
			sUploadQueue = new RequestQueue(new DiskBasedCache(cacheDir), network, 1);
			sUploadQueue.start();
		}
		return sUploadQueue;
	}

	public static final void startRequest(Context context, BaseGsonRequest<?> request) {
		ApiContext apiContext = ApiContext.create(context);
		request.setApiContext(apiContext);
		RequestQueue requestQueue = getRequestQueue(context);
		requestQueue.add(request);
	}

	public static final void startVolleyRequest(Context context, Request<?> request) {
		RequestQueue requestQueue = getRequestQueue(context);
		requestQueue.add(request);
	}

	public static final void startUpload(Context context, MultiPartRequest<?> request) {
		ApiContext apiContext = ApiContext.create(context);
		request.setApiContext(apiContext);
		RequestQueue requestQueue = getUploadRequestQueue(context);
		requestQueue.add(request);
	}

	/**
	 * 获取查看动态列表请求
	 * 
	 * @param token
	 *            登录帐号token，可不传
	 * @param listener
	 * @param errorListener
	 * @param uid
	 *            登录用户id
	 * @param puid
	 *            被查看用户id,小于等于0表示查看所有人的
	 * @param currentPage
	 *            当前页
	 * @param pageSize
	 *            每页显示大小
	 * @return
	 */
	public static GsonRequest<TrendsDataTO> getTrendsListRequest(Response.Listener<TrendsDataTO> listener, ErrorListener errorListener, String token, long uid,
			long puid, int currentPage, int pageSize) {
		HashMap<String, String> params = new HashMap<String, String>();
		if (!TextUtils.isEmpty(token)) {
			params.put(UriHelper.PARAMS_TOKEN, token);
		}
		if (uid > 0) {
			params.put(UriHelper.PARAMS_UID, String.valueOf(uid));
		}
		if (puid > 0) {
			params.put(UriHelper.PARAMS_PROF_UID, String.valueOf(puid));
		}
		params.put(UriHelper.PARAMS_PAGE_NO, String.valueOf(currentPage));
		params.put(UriHelper.PARAMS_PAGE_SIZE, String.valueOf(pageSize));
		String requestUrl = UriHelper.getTrendsListUrl();
		return new GsonRequest<TrendsDataTO>(Method.POST, requestUrl, listener, errorListener, params, TrendsDataTO.class);
	}

	public static GsonRequest<BaseTO> getUpdateBirthdayRequest(Listener<BaseTO> listener, ErrorListener errorListener, UserTO user, String newBirthday) {
		String requestUrl = UriHelper.getUpdateInfoUrl();
		HashMap<String, String> params = new HashMap<String, String>();
		params.put(UriHelper.PARAMS_UID, String.valueOf(user.getUid()));
		params.put(UriHelper.PARAMS_TOKEN, user.getToken());
		params.put(UriHelper.PARAMS_USERNAME, user.getUsername());
		params.put(UriHelper.PARAMS_MOOD, user.getMood());
		params.put(UriHelper.PARAMS_BIRTHDAY, newBirthday);
		params.put(UriHelper.PARAMS_PROVINCE, user.getStateName());
		params.put(UriHelper.PARAMS_CITY, user.getCityName());
		params.put(UriHelper.PARAMS_FISHING_AGE, String.valueOf(user.getFishAge()));
		params.put(UriHelper.PARAMS_FISHING_ADDRESS, user.getFishingAddress());
		return new GsonRequest<BaseTO>(Method.POST, requestUrl, listener, errorListener, params, BaseTO.class);
	}

	public static GsonRequest<BaseTO> getUpdateNicknameRequest(Listener<BaseTO> listener, ErrorListener errorListener, UserTO user, String nickname) {
		String requestUrl = UriHelper.getUpdateInfoUrl();
		HashMap<String, String> params = new HashMap<String, String>();
		params.put(UriHelper.PARAMS_UID, String.valueOf(user.getUid()));
		params.put(UriHelper.PARAMS_TOKEN, user.getToken());
		params.put(UriHelper.PARAMS_USERNAME, nickname);
		params.put(UriHelper.PARAMS_MOOD, user.getMood());
		params.put(UriHelper.PARAMS_BIRTHDAY, user.getBirthday());
		params.put(UriHelper.PARAMS_PROVINCE, user.getStateName());
		params.put(UriHelper.PARAMS_CITY, user.getCityName());
		params.put(UriHelper.PARAMS_FISHING_AGE, String.valueOf(user.getFishAge()));
		params.put(UriHelper.PARAMS_FISHING_ADDRESS, user.getFishingAddress());
		return new GsonRequest<BaseTO>(Method.POST, requestUrl, listener, errorListener, params, BaseTO.class);
	}

	public static GsonRequest<BaseTO> getUpdateSignRequest(Listener<BaseTO> listener, ErrorListener errorListener, UserTO user, String sign) {
		String requestUrl = UriHelper.getUpdateInfoUrl();
		HashMap<String, String> params = new HashMap<String, String>();
		params.put(UriHelper.PARAMS_UID, String.valueOf(user.getUid()));
		params.put(UriHelper.PARAMS_TOKEN, user.getToken());
		params.put(UriHelper.PARAMS_USERNAME, user.getUsername());
		params.put(UriHelper.PARAMS_MOOD, sign);
		params.put(UriHelper.PARAMS_BIRTHDAY, user.getBirthday());
		params.put(UriHelper.PARAMS_PROVINCE, user.getStateName());
		params.put(UriHelper.PARAMS_CITY, user.getCityName());
		params.put(UriHelper.PARAMS_FISHING_AGE, String.valueOf(user.getFishAge()));
		params.put(UriHelper.PARAMS_FISHING_ADDRESS, user.getFishingAddress());
		return new GsonRequest<BaseTO>(Method.POST, requestUrl, listener, errorListener, params, BaseTO.class);
	}

	public static GsonRequest<BaseTO> getUpdateDistrictRequest(Listener<BaseTO> listener, ErrorListener errorListener, UserTO user, String province, String city) {
		String requestUrl = UriHelper.getUpdateInfoUrl();
		HashMap<String, String> params = new HashMap<String, String>();
		params.put(UriHelper.PARAMS_UID, String.valueOf(user.getUid()));
		params.put(UriHelper.PARAMS_TOKEN, user.getToken());
		params.put(UriHelper.PARAMS_USERNAME, user.getUsername());
		params.put(UriHelper.PARAMS_MOOD, user.getMood());
		params.put(UriHelper.PARAMS_BIRTHDAY, user.getBirthday());
		params.put(UriHelper.PARAMS_PROVINCE, province);
		params.put(UriHelper.PARAMS_CITY, city);
		params.put(UriHelper.PARAMS_FISHING_AGE, String.valueOf(user.getFishAge()));
		params.put(UriHelper.PARAMS_FISHING_ADDRESS, user.getFishingAddress());
		return new GsonRequest<BaseTO>(Method.POST, requestUrl, listener, errorListener, params, BaseTO.class);
	}

	public static GsonRequest<BaseTO> getUpdateFishingAgeRequest(Listener<BaseTO> listener, ErrorListener errorListener, UserTO user, int fishingAge) {
		String requestUrl = UriHelper.getUpdateInfoUrl();
		HashMap<String, String> params = new HashMap<String, String>();
		params.put(UriHelper.PARAMS_UID, String.valueOf(user.getUid()));
		params.put(UriHelper.PARAMS_TOKEN, user.getToken());
		params.put(UriHelper.PARAMS_USERNAME, user.getUsername());
		params.put(UriHelper.PARAMS_MOOD, user.getMood());
		params.put(UriHelper.PARAMS_BIRTHDAY, user.getBirthday());
		params.put(UriHelper.PARAMS_PROVINCE, user.getStateName());
		params.put(UriHelper.PARAMS_CITY, user.getCityName());
		params.put(UriHelper.PARAMS_FISHING_AGE, String.valueOf(fishingAge));
		params.put(UriHelper.PARAMS_FISHING_ADDRESS, user.getFishingAddress());
		return new GsonRequest<BaseTO>(Method.POST, requestUrl, listener, errorListener, params, BaseTO.class);
	}

	public static GsonRequest<BaseTO> getUpdateFishingPlaceRequest(Listener<BaseTO> listener, ErrorListener errorListener, UserTO user, String fishingPlace) {
		String requestUrl = UriHelper.getUpdateInfoUrl();
		HashMap<String, String> params = new HashMap<String, String>();
		params.put(UriHelper.PARAMS_UID, String.valueOf(user.getUid()));
		params.put(UriHelper.PARAMS_TOKEN, user.getToken());
		params.put(UriHelper.PARAMS_USERNAME, user.getUsername());
		params.put(UriHelper.PARAMS_MOOD, user.getMood());
		params.put(UriHelper.PARAMS_BIRTHDAY, user.getBirthday());
		params.put(UriHelper.PARAMS_PROVINCE, user.getStateName());
		params.put(UriHelper.PARAMS_CITY, user.getCityName());
		params.put(UriHelper.PARAMS_FISHING_AGE, String.valueOf(user.getFishAge()));
		params.put(UriHelper.PARAMS_FISHING_ADDRESS, fishingPlace);
		return new GsonRequest<BaseTO>(Method.POST, requestUrl, listener, errorListener, params, BaseTO.class);
	}

	public static MultiPartRequest<UpdateAvatarDataTO> getUpdateAvatarRequest(Listener<UpdateAvatarDataTO> listener, ErrorListener errorListener, UserTO user,
			File avatarFile) {
		String requestUrl = UriHelper.getUpdateAvatarUrl();
		MultiPartRequest<UpdateAvatarDataTO> request = new MultiPartRequest<UpdateAvatarDataTO>(Method.POST, requestUrl, listener, errorListener,
				UpdateAvatarDataTO.class);
		request.addStringUpload(UriHelper.PARAMS_UID, String.valueOf(user.getUid()));
		request.addStringUpload(UriHelper.PARAMS_TOKEN, user.getToken());
		request.addFileUpload(UriHelper.PARAMS_AVATAR, avatarFile);
		return request;
	}

	public static MultiPartRequest<UpdateCoverDataTO> getUpdateCoverRequest(Listener<UpdateCoverDataTO> listener, ErrorListener errorListener, UserTO user,
			File coverFile) {
		String requestUrl = UriHelper.getUpdateCoverUrl();
		MultiPartRequest<UpdateCoverDataTO> request = new MultiPartRequest<UpdateCoverDataTO>(Method.POST, requestUrl, listener, errorListener,
				UpdateCoverDataTO.class);
		request.addStringUpload(UriHelper.PARAMS_UID, String.valueOf(user.getUid()));
		request.addStringUpload(UriHelper.PARAMS_TOKEN, user.getToken());
		request.addFileUpload(UriHelper.PARAMS_BACKGROUND, coverFile);
		return request;
	}

	public static MultiPartRequest<UpdateAvatarDataTO> getUpdateProfileRequest(Listener<UpdateAvatarDataTO> listener, ErrorListener errorListener, long uid,
			String token, String username, String mood, String birthday, String provinceName, String cityName, int fishingAge, String fishingPlace, int sex,
			File avatarFile) {
		String requestUrl = UriHelper.getUpdateInfoUrl();
		MultiPartRequest<UpdateAvatarDataTO> request = new MultiPartRequest<UpdateAvatarDataTO>(Method.POST, requestUrl, listener, errorListener,
				UpdateAvatarDataTO.class);
		request.addStringUpload(UriHelper.PARAMS_UID, String.valueOf(uid));
		request.addStringUpload(UriHelper.PARAMS_TOKEN, token);
		request.addStringUpload(UriHelper.PARAMS_USERNAME, username);
		request.addStringUpload(UriHelper.PARAMS_MOOD, mood);
		request.addStringUpload(UriHelper.PARAMS_BIRTHDAY, birthday);
		request.addStringUpload(UriHelper.PARAMS_PROVINCE, provinceName);
		request.addStringUpload(UriHelper.PARAMS_CITY, cityName);
		request.addStringUpload(UriHelper.PARAMS_FISHING_AGE, String.valueOf(fishingAge));
		request.addStringUpload(UriHelper.PARAMS_FISHING_ADDRESS, fishingPlace);
		request.addStringUpload(UriHelper.PARAMS_SEX, String.valueOf(sex));
		if (avatarFile != null && avatarFile.exists()) {
			request.addFileUpload(UriHelper.PARAMS_AVATAR, avatarFile);
		}
		return request;
	}

	public static GsonRequest<LoginTO> getThirdLoginRequest(Listener<LoginTO> listener, ErrorListener errorListener, String platform, String openid) {
		String url = UriHelper.getThirdLoginUrl();
		HashMap<String, String> params = new HashMap<String, String>();
		params.put(UriHelper.PARAMS_OPEN_ID, openid);
		params.put(UriHelper.PARAMS_PLATFORM, platform);
		StringBuffer buffer = new StringBuffer();
		buffer.append(Constants.SIGN_KEY).append("|").append(openid);
		String sign = MD5Util.encode(buffer.toString(), true);
		params.put(UriHelper.PARAMS_SIGN, sign);
		return new GsonRequest<LoginTO>(Method.POST, url, listener, errorListener, params, LoginTO.class);
	}

}
